package com.wswy.wzcx.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.Utils;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import timber.log.Timber;
import top.zibin.luban.Luban;

public class ImageUtils {

  private static final String CACHE_DIR = "luban_disk_cache";
  private static final String TAG = "ImageUtils";

  public static File compress(String filePath) throws IOException {
    File imageCacheDir = getImageCacheDir(Utils.getApp(), CACHE_DIR);
    String dir = null;

    if(imageCacheDir != null){
      dir = imageCacheDir.getAbsolutePath();
    }
    //File src = new File(filePath);
    //Luban.with(Utils.getApp()).ignoreBy(1024).setTargetDir(dir).load(filePath).get()
    //File dest = Luban.with(Utils.getApp()).ignoreBy(1024).setTargetDir(dir).get(filePath);
    File dest = Luban.with(Utils.getApp()).ignoreBy(1024).setTargetDir(dir).load(filePath).get().get(0);
    Timber.e("compress --> orig:"+(new File(filePath).length())+"  dest: "+dest.length()+"   "+(1024<<10));
    return dest;
  }

  public static void clearCache(){
    File externalCacheDir = Utils.getApp().getExternalCacheDir();

    if(externalCacheDir != null){
      File cacheDir = new File(externalCacheDir,CACHE_DIR);
      FileUtils.deleteDir(cacheDir);
    }

  }

  private static File getImageCacheDir(Context context, String cacheName) {
    File cacheDir = context.getExternalCacheDir();

    if (cacheDir != null) {
      File result = new File(cacheDir, cacheName);
      if (!result.mkdirs() && (!result.exists() || !result.isDirectory())) {
        return null;
      }
      return result;
    }
    return null;
  }

  public static String saveImage(Bitmap bitmap,String fileName){
    File dest = getImageCacheDir(Utils.getApp(),fileName);
    if(com.blankj.utilcode.util.ImageUtils.save(bitmap,dest, Bitmap.CompressFormat.JPEG,false)){
      return dest.getAbsolutePath();
    }
    return "";
  }

  public static boolean hasGif(String imgUrl){
    return !TextUtils.isEmpty(imgUrl) && imgUrl.endsWith(".gif");
  }

  public static void showImage(SimpleDraweeView view, String imgUrl,String imageSuffix){
    if (view != null) {

      String url = imgUrl;
      if (imageSuffix != null){
        url+=imageSuffix;
      }

      if (hasGif(imgUrl)) {
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
            .setOldController(view.getController())
            .setAutoPlayAnimations(true)
            .setUri(UriUtil.parseUriOrNull(url))
            .build();

        view.setController(draweeController);

      } else {
        view.setImageURI(url);
      }
    }
  }

  public static void showImage(SimpleDraweeView view, String imgUrl) {
    showImage(view, imgUrl,null);
  }


  public static void showImage(SimpleDraweeView view, File file) {
    if (view != null) {
      if (file != null && file.getName().endsWith(".gif")) {
        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
            .setOldController(view.getController())
            .setAutoPlayAnimations(true)
            .setUri(UriUtil.getUriForFile(file))
            .build();

        view.setController(draweeController);

      } else {
        view.setImageURI(UriUtil.getUriForFile(file));
      }
    }
  }

  public static Bitmap getMapMarkBitmap(){

    Rect rect = new Rect(0,0,SizeUtils.dp2px(133),SizeUtils.dp2px(133));

    Bitmap bitmap = Bitmap.createBitmap(rect.width(),rect.height(),Config.ARGB_4444);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setColor(ContextCompat.getColor(AppContext.getContext(), R.color.colorPrimary_10));
    paint.setStyle(Style.FILL);

    Canvas canvas = new Canvas(bitmap);

    canvas.drawCircle(rect.centerX(),rect.centerY(),rect.width()/2,paint);

    Bitmap markBitmap = BitmapFactory.decodeResource(AppContext.getContext().getResources(),R.drawable.icon_marka);

    canvas.drawBitmap(markBitmap,rect.centerX()-markBitmap.getWidth()/2,rect.centerY()-markBitmap.getHeight() + SizeUtils.dp2px(10),null);

    return bitmap;
  }
}
